package vip.websky.project.common.error;


import com.xiaoleilu.hutool.util.StrUtil;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * 公用运行期异常
 *
 * @author: YangYong
 * @createAt: 2017/6/11-11:46
 */
public class CommonsRuntimeException extends RuntimeException implements Serializable {
    private String code;
    private String message;

    public String getCode() {
        return code;
    }

    public void setCode( String code ) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage( String message ) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "CommonsRuntimeException{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                '}';
    }

    /**
     * 错误信息模板
     */
    private final static String MSG_TEMPLATE = "错误码:{0}, 描述:{1}, 异常信息:{2}";

    public CommonsRuntimeException( String code, String message){
        this(code,message,null);
    }

    public CommonsRuntimeException( String code, String message, Throwable cause){
        super(getExceptionMessage(code,message,cause),cause);
        this.code = code;
        this.message = message;
    }

    private static String getExceptionMessage(String code,String message,Throwable cause){
        String extraMessage = "";
        if (null != cause && StrUtil.isNotBlank(cause.getMessage())){
            extraMessage = cause.getMessage();
        }
        return MessageFormat.format(MSG_TEMPLATE,code,message,extraMessage);
    }
}
